package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetModularList;
import com.rjwh.dingdong.client.bean.localbean.ModularData;
import com.rjwh.dingdong.client.bean.localbean.ModularTab;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.CommunityModel;
import com.rjwh.dingdong.client.mvp.presenter.ICommunityPresenter;
import com.rjwh.dingdong.client.mvp.view.CommunityView;

import java.util.List;

/**
 * Created by Kay on 2016-6-27.
 */
public class CommunityPresenter extends BasePresenter<CommunityView> implements ICommunityPresenter {
    private CommunityModel model;

    public CommunityPresenter(Context context, CommunityView mMvpView) {
        super(context, mMvpView);
        this.model = new CommunityModel(this);
    }

    /**
     * 获取模块列表
     */
    @Override
    public void doGetModularList() {
        if (isViewAttached()) {
            getMvpView().hideErrorView();
            getMvpView().showLoadingView();
            model.doGetModularList();
        }
    }

    /**
     * 请求结果
     *
     * @param businessType
     * @param mAsResult
     * @param isRefresh
     */
    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (getMvpView() != null) {
            getMvpView().hideLoadingView();
            switch (businessType) {
                case NetConstant.COMMUNITY_GETMODULARLIST:
                    ResGetModularList getModularList = (ResGetModularList) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && getModularList != null) {
                        getMvpView().hideErrorView();
                        ModularData data = getModularList.getData();
                        if (data != null) {
                            List<ModularTab> modularlist = data.getModularlist();
                            String unread = data.getUnread();
                            MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_COMMUNITY_MSG_UNREAD, unread);
                            String keyword = data.getKeyword();
                            if (modularlist != null && !modularlist.isEmpty() && !AsStrUtils.isEmpty(keyword)) {
                                getMvpView().setModularData(modularlist, keyword);
                            }
                        }
                    } else {
                        getMvpView().showErrorView();
                    }
                    break;

                default:
                    break;
            }
        }

    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }


//    ;
//
//    private BroadcastReceiver updateReceiver = new BroadcastReceiver() {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            if (intent == null) {
//                return;
//            }
//            PushMsg pm = (PushMsg) intent.getSerializableExtra("pushMsg");
//            if (pm != null && pm.getCustom_content() != null) {
//                String attr = pm.getCustom_content().getAttr();
//                if (!AsStrUtils.isEmpty(attr)) {
//                    if (PushConfig.PUSH_COMMUNITY_ZAN.equals(attr) || PushConfig.PUSH_COMMUNITY_REPLY.equals(attr)) {
//                        // 社区推送消息红点
//                        MyApplication.spUtil.setStrPreference(MyApplication.app, LocalConstant.SP_COMMUNITY_MSG_UNREAD, "1");
//                        getMvpView().updateTip();
//                    }
//                }
//            }
//        }
//    };
}
